﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TaxiDispatchClient
{
    public partial class TaxiParameterSetForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiParameterSetForm()
        {
            InitializeComponent();
            BindConfig();
            lb_title.Text = "所有车辆默认参数";
        }

        private int _taxiId = -1;
        private Model.TaxiInfo _model;


        public TaxiParameterSetForm(int taxiId)
        {
            InitializeComponent();
            lb_title.Text = "ID为"+taxiId+"的出租车参数配置";
            _taxiId = taxiId;
            try
            {
                _model = MainForm.PositionService.TaxiGetModel(taxiId);                                                   
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);                
            }                       
        }

        private void TaxiParameterSetForm_Load(object sender, EventArgs e)
        {
            BindConfigByTaix();
        }

        private void BindConfigByTaix()
        {
            if (_model != null && _model.SpeedOut != 0
                  && _model.StopOutTime != 0 && _model.FatigueDravingTime != 0)
            {
                textEdit_speed.Text = _model.SpeedOut.ToString();
                checkEdit_speed.CheckState = _model.SpeedOutIsOpen == 1
                                                       ? CheckState.Checked :
                                                       CheckState.Unchecked;
                textEdit_stopTimeOut.Text = _model.StopOutTime.ToString();
                checkEdit_stopTimeOut.CheckState = _model.StopOutIsOpen == 1
                                                       ? CheckState.Checked
                                                       : CheckState.Unchecked;
                textEdit_power.Text = _model.FatigueDravingTime.ToString();
                checkEdit_power.CheckState = _model.FatigueDravingIsOpen == 1
                                                          ? CheckState.Checked
                                                          : CheckState.Unchecked;
            }
            else
            {
                BindConfig();
            }
        }

        private void BindConfig()
        {
            string[] config = Common.OperateConfig.GetClientTaxiWarmingConfig();
            textEdit_speed.Text = config[0];
            checkEdit_speed.CheckState = config[1] == "1"
                                                   ? CheckState.Checked :
                                                   CheckState.Unchecked;
            textEdit_stopTimeOut.Text = config[2];
            checkEdit_stopTimeOut.CheckState = config[3] == "1"
                                                   ? CheckState.Checked
                                                   : CheckState.Unchecked;
            textEdit_power.Text = config[4];
            checkEdit_power.CheckState = config[5] == "1"
                                                      ? CheckState.Checked
                                                      : CheckState.Unchecked;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if(btn_save.Tag.ToString()=="保存")
            {    
                if(!CheckPassed()) return;
                // _taxiId 为-1时 设置出租车报警配置文件  不为 -1 时对单辆出租车设置
                if (_taxiId == -1)
                {
                    string[] config = new string[6];
                    config[0] = textEdit_speed.Text;
                    config[1] = checkEdit_speed.Checked ? "1" : "0";
                    config[2] = textEdit_stopTimeOut.Text;
                    config[3] = checkEdit_stopTimeOut.Checked ? "1" : "0";
                    config[4] = textEdit_power.Text;
                    config[5] = checkEdit_power.Checked ? "1" : "0";
                    Common.OperateConfig.SaveClientTaxiWarmingConfig(config);
                    XtraMessageBox.Show("修改成功！");
                }
                else
                {
                   if(_model!=null)
                   {
                       _model.SpeedOut = int.Parse(textEdit_speed.Text);
                       _model.SpeedOutIsOpen = checkEdit_speed.Checked ? 1: 0;
                       _model.StopOutTime = int.Parse(textEdit_stopTimeOut.Text);
                       _model.StopOutIsOpen = checkEdit_stopTimeOut.Checked ? 1 : 0;
                       _model.FatigueDravingTime = int.Parse(textEdit_power.Text);
                       _model.FatigueDravingIsOpen = checkEdit_power.Checked ? 1 : 0;
                   }
                   if(MainForm.PositionService.TaxiInfoUpdate(_model))
                   {
                       XtraMessageBox.Show("修改成功！");
                   }
                   else
                   {
                       XtraMessageBox.Show("修改失败，可能是未找到该车辆信息或服务器通讯故障");
                   }
                }
                ResetControl(false);
                btn_save.Tag = "修改";
                btn_save.Image = Properties.Resources.UserForm_btnmodify001;
                btn_cancel.Visible = false;
            }
            else
            {
                ResetControl(true);
                btn_save.Tag = "保存";
                btn_save.Image = Properties.Resources.UserForm_btnsave001;
                btn_cancel.Visible = true;
            }
        }

        private void ResetControl(bool enable)
        {
            textEdit_speed.Enabled = enable;
            textEdit_power.Enabled = enable;
            textEdit_stopTimeOut.Enabled = enable;
            checkEdit_speed.Enabled = enable;
            checkEdit_stopTimeOut.Enabled = enable;
            checkEdit_power.Enabled = enable;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            btn_cancel.Visible = false;
            ResetControl(false);
            btn_save.Tag = "修改";
            btn_save.Image = Properties.Resources.UserForm_btnmodify001;
            BindConfigByTaix();
        }


        private bool CheckPassed()
        {
           if(textEdit_speed.Text.Trim()=="")
           {
               XtraMessageBox.Show("超速速度不能为空！");
               textEdit_speed.Focus();
               return false;
           }
           if (textEdit_speed.Text.Trim() == "0")
           {
               XtraMessageBox.Show("超速速度需大于0！");
               textEdit_speed.Focus();
               return false;
           }           
           if (textEdit_stopTimeOut.Text.Trim() == "")
           {
               XtraMessageBox.Show("超时停车时间不能为空！");
               textEdit_stopTimeOut.Focus();
               return false;
           }
           if (textEdit_stopTimeOut.Text.Trim() == "0")
           {
               XtraMessageBox.Show("超时停车时间需大于0！");
               textEdit_stopTimeOut.Focus();
               return false;
           }
           if (textEdit_power.Text.Trim() == "")
           {
               XtraMessageBox.Show("电池剩余时间不能为空！");
               textEdit_power.Focus();
               return false;
           }
           if (textEdit_power.Text.Trim() == "0")
           {
               XtraMessageBox.Show("电池剩余时间需大于0");
               textEdit_power.Focus();
               return false;
           }
           return true;
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

      
    }
}